package com.qymerge.manage.system.dao.impl;

import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.system.dao.IDatabackDao;
import com.qymerge.manage.system.dto.DatabackDto;
import com.qymerge.manage.system.ro.DatabackRO;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.*;

/**
 *<p>Title: ProductDaoImpl</p>
 *<p>Description: 数据库备份</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 17:39
 */
@Repository
public class DatabackDaoImpl extends BaseDao implements IDatabackDao {

    /**
     * 查询所有分页
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public List list(DatabackDto ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.databack);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append(" and id=:id");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getFiledata())) {
            sql.append(" and FileData like :filedata");
            paramMap.put("filedata", "%" + ro.getFiledata() + "%");
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql.append(" and CreateDate BETWEEN :create_time_begin AND :create_time_end");
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        // 分页数据获取
        sql.append(" order by id desc limit :skip,:sizes");
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<DatabackRO>(DatabackRO.class));
    }

    /**
     * 所有数量
     *
     * @param
     * @return
     */
    @Override
    public int total(DatabackDto ro) {
        StringBuffer sql = new StringBuffer("select count(1) from databack where 1=1");
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(this.getJdbcTemplate());
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append(" and id=:id");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getFiledata())) {
            sql.append(" and FileData like :filedata");
            paramMap.put("FileData", "%" + ro.getFiledata() + "%");
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql.append(" and CreateDate BETWEEN :create_time_begin AND :create_time_end");
            paramMap.put("create_time_begin", ro.getCreate_time_begin());
            paramMap.put("create_time_end", ro.getCreate_time_end());
        }

        return jdbc.queryForObject(sql.toString(), paramMap, Integer.class);
    }

    @Override
    public DatabackRO findOneById(Long id) {
        StringBuffer sql = new StringBuffer(ParamKey.NameSpace.TableSelect.databack);
        sql.append("and id=?");
        RowMapper<DatabackRO> rowMapper = new BeanPropertyRowMapper<DatabackRO>(DatabackRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int install(DatabackRO ro) {
        List querList = new ArrayList();
        StringBuilder sql = new StringBuilder("insert into databack(FileData,CreateDate) values(?,?)");

        querList.add(ro.getFiledata());
        querList.add(ro.getCreatedate());

        return getJdbcTemplate().update(sql.toString(), querList.toArray());
    }

    @Override
    public int update(DatabackRO ro) {
        List params = new LinkedList();
        StringBuilder builder = new StringBuilder("update databack set FileData=?,CreateDate=? ");

        params.add(ro.getFiledata());
        params.add(ro.getCreatedate());

        builder.append("where id=?");
        params.add(ro.getId());

        return getJdbcTemplate().update(builder.toString(), params.toArray());
    }

    @Override
    public int removeList(List<Long> ids) {
        // 删除
        String sql="delete from databack where id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };
        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }

}
